<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><%@include file="/web/base/header.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<fieldset class="layui-elem-field">
		<legend>医生信息</legend>
		<div class="layui-field-box">
			<form class="layui-form">
				
				<br /> <label class="layui-form-label">医生姓名</label>
				<div class="layui-inline">
					<input type="text" name="doctorname" placeholder="请输入医生姓名"
						autocomplete="off" class="layui-input">
				</div>
				<br /> <label class="layui-form-label">医生年龄</label>
				<div class="layui-inline">
					<input type="text" name="doctorage" placeholder="请输入医生年龄"
						autocomplete="off" class="layui-input">
				</div>
				<br /> <label class="layui-form-label">医生职称</label>
				<div class="layui-inline">
					<input type="text" name="doctortitle" placeholder="请输入医生职称"
						autocomplete="off" class="layui-input">
				</div>
				<br /> <label class="layui-form-label">科室名字</label>
				<div class="layui-inline">
					<input type="text" name="departmentname" placeholder="请输入所属科室"
						autocomplete="off" class="layui-input">
				</div>
				&nbsp;&nbsp; <input id="sele" type="submit" class="layui-btn" lay-submit
					lay-filter="select" value="查找" /> <input class="layui-btn"
					id="tanadd" value="添加">
				
<!-- 				<input name="action" type="hidden" value="select" /> -->
				<input name="action" type="hidden" value="page"/>
				<input name="curr" type="hidden" value="1"/>
				<input name="limit" type="hidden" value="10"/>
			</form>

		</div>
		<table class="layui-table">

			<thead>
				<tr>
					<th>序号</th>
					<th>医生ID</th>
					<th>医生姓名</th>
					<th>医生年龄</th>
					<th>医生职称</th>
					<th>所属科室</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody id="doctortbody"></tbody>
		</table>
		
		<div id="page"></div>


	</fieldset >
	<script type="text/javascript" >
	$("#tanadd").click(tan);
	
	formon('submit(select)',base.app+'/DoctorServlet','json',
			function(data2){
			var tar=JSON.stringify(data2);
			resp = JSON.parse(tar);
			setpage("page",resp["count"],$("input[name='curr']").val(),$("input[name='limit']").val(),
					function(obj,first){
				$("input[name='curr']").val(obj.curr);
				$("input[name='limit']").val(obj.limit);
				if(!first){
					f5("input[value='查找']");
				}
			});
            var list = resp["list"];
			var html="";
				$.each(list,function(i,dom){
					var id=dom.doctorId?dom.doctorId:'';
					var name=dom.doctorName?dom.doctorName:'';
					var age=dom.doctorAge?dom.doctorAge:'';
					var title=dom.doctorTitle?dom.doctorTitle:'';
					var departmentname=dom.departmentName?dom.departmentName:'';
					
					html+="<tr>"+
						"<td>"+ (i+1) +"</td>"+
						"<td>"+id+"</td>"+
						"<td>"+name+"</td>"+
						"<td>"+age+"</td>"+
						"<td>"+title+"</td>"+
						"<td>"+departmentname+"</td>"+
						"<td>"+
						"<a href='javascript:del(\"" + id + "\");' class='layui-btn'>删除</a>"+
						"<a href='javascript:tanupd(\"" + id + "\");' class='layui-btn'>修改</a>"+
						"<a href='javascript:tanup(\"" + id + "\");' class='layui-btn'>上传图片</a>"+
						"</td></tr>";
				})
				$("#doctortbody").html(html);
				
		})
		
		f5("input[value='查找']");
		
	function tan() {
		layer.open({
			type: 1,
			area: ['400px', '400px'],
			content: $("#addhtml").html(),
			title: "添加",
			success: function(index) {
				form.render();
				formon('submit(add)',base.app+'/DoctorServlet','text',
					function(data2){
						if("1"==data2){
							alert("添加成功");
							closeThis();
							f5("input[value='查找']");
						}
						if("0"==data2){
							alert("添加失败");
						}
						if("2"==data2){
							alert("请输入数据");
						}
						if("3"==data2){
							alert("医生ID已存在")
						}
					})
				
			}
		});
		
	}
	function tanupd(id){
		layer.open({
			type: 1,
			area: ['400px', '400px'],
			content: $("#updhtml").html(),
			title: "修改",
			success: function(index) {
				var data1={doctorid:id,action:"selectmodel"};
				ajax1(data1,base.app+'/DoctorServlet','json',
						function(data3){
							var tar=JSON.stringify(data3);
							resp = JSON.parse(tar);
		            		var model = resp["selectmodel"];
							var indata={
									doctorname:model.doctorName,
									doctorage:model.doctorAge,
									doctortitle:model.doctorTitle
							};
							form.val("updform", indata);
							
				});
				
				form.render();
				formon('submit(upd)',base.app+'/DoctorServlet','text',
					function(data2){
						if("1"==data2){
							alert("修改成功");
							f5("input[value='查找']");
							closeThis();
						}
						if("2"==data2){
							alert("请输入数据");
						}
						if("0"==data2){
							alert("修改失败");
						}
					})
					
			}
		});
	}
	function del(id){
		if(confirm("确定删除")){
			var data={delid:id,action:"del"};
			ajax1(data,base.app+'/DoctorServlet',"text",function(data){
				if("1"==data){
					alert("删除成功");
				}else{
					alert("删除失败");
				}
			})
			f5("input[value='查找']");
		}
	}
	function tanup(id){
		var upload = layui.upload;
		layer.open({
			type: 1,
			area: ['400px', '400px'],
			content: $("#upjpg").html(),
			title: "上传图片",
			success: function(index) {
					showpic(id);
					var uploadInst = upload.render({
		    			elem: '#upbut',
		    			url: base.app+'/uploadServlet',
		    			data:{doctorid:id},
		    			done: function(res){
		    				var tar=JSON.stringify(res);
							resp = JSON.parse(tar);
			        		var result = resp["result"];
			        		var image=resp["image"];
					    	if(result=="1"){
					    		alert("添加成功");
					    		showPic(image,id);
		    				}
		    			}
		  			})
			}
		})
	}
	function showPic(image,id){
		var html=""+
				"<tr><td>"+image+"</td>"+
				"<td><img src='/jpg/"+image+"' class='layui-upload-img'/></td>"+
				"<td><a href='javascript:delpic(\"" + id + "\");' class='layui-btn'>删除图片</a></td></tr>";
				
		$("#uptbody").html(html);
	}
	function delpic(id){
		ajax1({action:'updjpg',doctorid:id},base.app+'/DoctorServlet','text',function(res){
			if("1"==res){
				alert("删除成功");
				showpic(id);
			}
		})
	}
	function showpic(id){
		ajax1({action:'selectmodel',doctorid:id},base.app+'/DoctorServlet','json',function(res){
			var tar=JSON.stringify(res);
			resp = JSON.parse(tar);
    		var result = resp["selectmodel"];
			var image=result.doctorJpg;
			if(image){
				showPic(image,id);
			}
			else{
				$("#uptbody").html('');
			}
		})
	}
	
	</script>
	<script type="text/html" id="addhtml">
		<form class="layui-form">
						<div class="layui-form-item">
							<label class="layui-form-label">医生ID</label>
							<div class="layui-inline">
								<input type="text" name="doctorid" required lay-verify="required" placeholder="请输入科室名字" autocomplete="off"
									class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">医生姓名</label>
							<div class="layui-inline">
								<input type="text" name="doctorname" required lay-verify="required" placeholder="请输入科室地址" autocomplete="off"
									class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">医生年龄</label>
							<div class="layui-inline">
								<input type="text" name="doctorage" required lay-verify="required" placeholder="请输入科室电话" autocomplete="off"
									class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">医生职称</label>
							<div class="layui-inline">
								<input type="text" name="doctortitle" required lay-verify="required" placeholder="请输入科室电话" autocomplete="off"
									class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">所属科室</label>
							<div class="layui-inline">
								<input type="text" name="departmentname" required lay-verify="required" placeholder="请输入科室电话" autocomplete="off"
									class="layui-input">
							</div>
						</div>

						
						<div class="layui-form-item">
							<div class="layui-input-block">
								<button class="layui-btn" lay-submit lay-filter="add">提交</button>
								<button type="reset" class="layui-btn layui-btn-primary">重置</button>
							</div>
						</div>
						<input type="hidden" name="action" value="add"/>

					</form>

	</script>
	
	<script type="text/html" id="updhtml">
		<form class="layui-form" lay-filter="updform">
						
						<div class="layui-form-item">
							<label class="layui-form-label">医生姓名</label>
							<div class="layui-inline">
								<input type="text" name="doctorname" required lay-verify="required" placeholder="请输入科室地址" autocomplete="off"
									class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">医生年龄</label>
							<div class="layui-inline">
								<input type="text" name="doctorage" required lay-verify="required" placeholder="请输入科室电话" autocomplete="off"
									class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">医生职称</label>
							<div class="layui-inline">
								<input type="text" name="doctortitle" required lay-verify="required" placeholder="请输入科室电话" autocomplete="off"
									class="layui-input">
							</div>
						</div>
						
						
						<div class="layui-form-item">
							<div class="layui-input-block">
								<button class="layui-btn" lay-submit lay-filter="upd">提交</button>
								<button type="reset" class="layui-btn layui-btn-primary">重置</button>
							</div>
						</div>
						<input type="hidden" name="action" value="upd"/>
					</form>

	</script>
	<script type="text/html" id="upjpg">
	<fieldset class="layui-elem-field">
		<button type="button" class="layui-btn" id="upbut">
  			<i class="layui-icon">&#xe67c;</i>上传图片
		</button>
		<div class="layui-upload-list">
			<table class="layui-table">
				<thead><tr>
					<th>文件名</th>
					<th>图片</th>
					<th>操作</th></tr>
				</thead>
				<tbody id="uptbody"></tbody>
			</table>
		</div>
	</fieldset>
	</script>
</body>
</html>